//
//  HamatonXsqDisplayViewController.swift
//  hamaton-app
//
//  Created by intyt on 2024/5/6.
//  显示器设置

import UIKit
import SnapKit
import Toast_Swift


class HamatonXsqDisplayViewController: HamatonBaseViewController {
    var xsqDisplaySettingModel: XsqDisplaySettingModel = XsqDisplaySettingModel()
    weak var delegate: HamatonXsqDisplayDelegate?
    // 压力单位
    var YalidanweiBaudRateModel: HamatonCarModel? {
        didSet {
            yalidanweiRightLabel.text = YalidanweiBaudRateModel?.name ?? ""
        }
    }
    // 温度单位
    var WendudanweiBaudRateModel: HamatonCarModel? {
        didSet {
            wendudanweiRightLabel.text = WendudanweiBaudRateModel?.name ?? ""
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .clear
        navigationItem.title = "显示器设置"
        let margTop: Float = 15
        let margRight: Float = -20
        let margLeft: Float = 20
        // 压力单位
        view.addSubview(yalidanweiLabel)
        view.addSubview(yalidanweiRightLabel)
        view.addSubview(yalidanweiRightImg)
        view.addSubview(yalidanweiRightButton)
        view.addSubview(lineView1)
        // 温度单位
        view.addSubview(wendudanweiLabel)
        view.addSubview(wendudanweiRightLabel)
        view.addSubview(wendudanweiRightImg)
        view.addSubview(wendudanweiRightButton)
        view.addSubview(lineView2)
        // 休眠时间
        view.addSubview(xiumianshijianLabel)
        view.addSubview(xiumianshijianTextField)
        view.addSubview(lineView3)
        // 声音报警
        view.addSubview(shengyinbaojingLabel)
        view.addSubview(shengyinbaojingSender)
        view.addSubview(lineView4)
        // 挂车数量
        view.addSubview(guacheshuliangLabel)
        view.addSubview(guacheshuliangTextField)
        view.addSubview(lineView5)
        // 头车MAC
        view.addSubview(toucheMACLabel)
        view.addSubview(toucheMACTextField)
        view.addSubview(lineView6)
       
        // 保存
        view.addSubview(saveBtn)
        
        // 压力单位
        yalidanweiLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        
        yalidanweiRightLabel.snp.makeConstraints { make in
            make.centerY.equalTo(yalidanweiLabel.snp.centerY)
            make.right.equalTo(yalidanweiRightImg.snp.left).offset(margRight)
            make.width.equalTo(60)
            make.height.equalTo(20)
        }
        
        lineView1.snp.makeConstraints { make in
            make.top.equalTo(yalidanweiLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        yalidanweiRightImg.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(margTop+4)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        
        yalidanweiRightButton.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(margTop)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView1.snp.top).offset(0)
        }
        // 温度单位
        wendudanweiLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        wendudanweiRightImg.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop+4)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        wendudanweiRightLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop)
            make.right.equalTo(wendudanweiRightImg.snp.left).offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView2.snp.makeConstraints { make in
            make.top.equalTo(wendudanweiLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        wendudanweiRightButton.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView2.snp.top).offset(0)
        }
        // 休眠时间
        xiumianshijianLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView2.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        xiumianshijianTextField.snp.makeConstraints { make in
            make.top.equalTo(lineView2.snp.bottom).offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView3.snp.makeConstraints { make in
            make.top.equalTo(xiumianshijianLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        // 声音报警
        shengyinbaojingLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView3.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }

        shengyinbaojingSender.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(margRight)
            make.height.equalTo(30)
            make.centerY.equalTo(shengyinbaojingLabel.snp.centerY)
        }

        lineView4.snp.makeConstraints { make in
            make.top.equalTo(shengyinbaojingLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 挂车数量
        guacheshuliangLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView4.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        guacheshuliangTextField.snp.makeConstraints { make in
            make.top.equalTo(lineView4.snp.bottom).offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView5.snp.makeConstraints { make in
            make.top.equalTo(guacheshuliangLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 头车MAC
        toucheMACLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView5.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        toucheMACTextField.snp.makeConstraints { make in
            make.top.equalTo(lineView5.snp.bottom).offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView6.snp.makeConstraints { make in
            make.top.equalTo(toucheMACLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 保存
        saveBtn.snp.makeConstraints { make in
            make.top.equalTo(lineView6.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(26)
            make.right.equalToSuperview().offset(-26)
            make.height.equalTo(40)
        }
    }

    // 压力单位
    private lazy var yalidanweiLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "压力单位"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    lazy var yalidanweiRightImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        return view
    }()
    lazy var yalidanweiRightButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onSelectCgqType(btn:)), for: .touchUpInside)
        return button
    }()
    lazy var yalidanweiRightLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = YalidanweiOption[xsqDisplaySettingModel.yalidanweiStr]
        label.backgroundColor = .clear
        label.textAlignment = .right
        return label
    }()
    private lazy var lineView1: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    
    // 温度单位
    private lazy var wendudanweiLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "温度单位"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    lazy var wendudanweiRightImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        return view
    }()
    lazy var wendudanweiRightButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onSelectCgqType(btn:)), for: .touchUpInside)
        return button
    }()
    lazy var wendudanweiRightLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = WendudanweiOption[xsqDisplaySettingModel.wendudanweiStr]
        label.backgroundColor = .clear
        label.textAlignment = .right
        return label
    }()
    private lazy var lineView2: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 休眠时间
    private lazy var xiumianshijianLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "休眠时间"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var xiumianshijianTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(16)
        textField.text = xsqDisplaySettingModel.xiumianshijianStr
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    private lazy var lineView3: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 声音报警
    private lazy var shengyinbaojingLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "声音报警"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()

    private lazy var shengyinbaojingSender: UISegmentedControl = {
        let segmentedControl = UISegmentedControl(items: ["ON", "OFF"])
        // 选项颜色
        segmentedControl.tintColor = UIColor.blue
        // 默认选中第二项
        segmentedControl.selectedSegmentIndex = xsqDisplaySettingModel.shengyinbaojingStr == "00" ? 1 : 0
        // 点击事件
        segmentedControl.addTarget(self, action: #selector(segmentedControlChange(_ :)), for: .valueChanged)
        // 2
        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: UIControl.State.normal)
        // 3
        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.blue], for: UIControl.State.selected)
        // 4
        segmentedControl.backgroundColor = .white
        return segmentedControl
    }()
    private lazy var lineView4: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 挂车数量
    private lazy var guacheshuliangLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "挂车数量"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var guacheshuliangTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(16)
        textField.text = xsqDisplaySettingModel.guacheshuliangStr
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    private lazy var lineView5: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    
    // 头车MAC
    private lazy var toucheMACLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "头车MAC"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var toucheMACTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(16)
        textField.text = xsqDisplaySettingModel.toucheMACStr
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    private lazy var lineView6: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    
    // 保存设置
    private lazy var saveBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle(NSLocalizedString("保存设置", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(16, bold: true)
        button.addTarget(self, action: #selector(onClickSaveButton), for: .touchUpInside)
        button.backgroundColor  = Theme.Color.Background.main
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        return button
    }()
    
    // 输入框内容改变
    @objc func textFieldDidChange(tf: UITextField) {
        if tf == xiumianshijianTextField {
            var tftext = tf.text ?? ""
            tftext = String(tftext.prefix(8))
            xsqDisplaySettingModel.xiumianshijianStr = tftext
        } else if tf == guacheshuliangTextField {
            var tftext = tf.text ?? ""
            xsqDisplaySettingModel.guacheshuliangStr = tftext
        } else if tf == toucheMACTextField {
            var tftext = tf.text ?? ""
            tftext = String(tftext.replacingOccurrences(of: ":", with: "").groupCharactersByTwoWithColon().prefix(17))
            tf.text = tftext
            xsqDisplaySettingModel.toucheMACStr = tftext
        }
    }

    // 选择
    @objc func onSelectCgqType(btn: UIButton) {
        let vc: HamatonCarSettingSelectViewController = HamatonCarSettingSelectViewController()
        if btn == yalidanweiRightButton { // 压力单位
            vc.selectTypeEnumFun(selectTypeEnum: .i)
            weak var weakSelf = self
            vc.backAction = {  (model: HamatonCarModel) -> Void in
                weakSelf?.YalidanweiBaudRateModel = model
                weakSelf?.xsqDisplaySettingModel.yalidanweiStr = model.id
            }
            self.navigationController?.pushViewController(vc, animated: false)
        } else if btn == wendudanweiRightButton { // 温度单位
            vc.selectTypeEnumFun(selectTypeEnum: .j)
            weak var weakSelf = self
            vc.backAction = {  (model: HamatonCarModel) -> Void in
                weakSelf?.WendudanweiBaudRateModel = model
                weakSelf?.xsqDisplaySettingModel.wendudanweiStr = model.id
            }
            self.navigationController?.pushViewController(vc, animated: false)
        }
    }
    
    @objc func segmentedControlChange(_ segmented: UISegmentedControl) {
        if segmented == shengyinbaojingSender { // 声音报警
            xsqDisplaySettingModel.shengyinbaojingStr = segmented.selectedSegmentIndex == 0 ? "01" : "00"
        }
    }
    
    // 保存显示器设置
    @objc func onClickSaveButton() {
        self.view.makeToastActivity(.center)
        delegate?.xsqDisplaySave(xsqDisplaySettingModel)
    }
    
    // 显示器头车mac设置成功
    func macSettingSuccess() {
        self.view.hideToastActivity()
        self.view.makeToast("设置成功", duration: 3.0, position: .center)
    }
}
